package com.rongshu.view.userInfo.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.rongshu.R;
import com.rongshu.utils.DateUtils;
import com.rongshu.view.userInfo.entity.RecyclerStationInfo;
import com.rongshu.view.userInfo.listener.OnRecyclerStationItemClickListener;
import com.widget.SwipeMenuView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class RecyclerStationAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext = null;
    private List<RecyclerStationInfo.ResponseBean> recyclerStationInfos = null;
    //interface
    private OnRecyclerStationItemClickListener listener = null;

    public RecyclerStationAdapter(Context context) {
        this.mContext = context;
    }

    public void setRecyclerStationInfos(List<RecyclerStationInfo.ResponseBean> list) {
        this.recyclerStationInfos = list;
    }

    public void addRecyclerStationInfos(List<RecyclerStationInfo.ResponseBean> list) {
        this.recyclerStationInfos.addAll(list);
    }

    public List<RecyclerStationInfo.ResponseBean> getRecyclerStationInfos() {
        return this.recyclerStationInfos;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_recycler_station_item_view, parent, false);
        return new RecyclerStationViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {
        final RecyclerStationInfo.ResponseBean bean = recyclerStationInfos.get(position);
        final RecyclerStationViewHolder holder = (RecyclerStationViewHolder) viewHolder;
        //image
        String topImg = bean.getTopImg();
        if (TextUtils.isEmpty(topImg)) {
            holder.articleImg.setVisibility(View.GONE);
        } else {
            holder.articleImg.setVisibility(View.VISIBLE);
            //img
            RequestOptions options = new RequestOptions()
                    .centerCrop()
                    .placeholder(R.drawable.penpal_default_logo);
            Glide.with(mContext)
                    .load(topImg)
                    .apply(options)
                    .into(holder.articleImg);
        }
        //title
        holder.articleTitle.setText(bean.getTitle());
        //content
        holder.articleContent.setText(bean.getContents_header());
        //time
        holder.txt_time.setText("字数" + bean.getNumber() + "  " + DateUtils.ConvertDate(bean.getCreate_datetime()));
        //listener
        holder.contentLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onItemClick(bean);
                }
            }
        });
        holder.btnRecover.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onRecoverClick(bean.getId());
                    holder.swipemenuView.quickClose();
                }
            }
        });
        holder.btnDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onDeleteClick(bean.getId());
                    holder.swipemenuView.quickClose();
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return recyclerStationInfos == null ? 0 : recyclerStationInfos.size();
    }

    public static class RecyclerStationViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.content_layout)
        LinearLayout contentLayout;
        @BindView(R.id.article_img)
        ImageView articleImg;
        @BindView(R.id.article_title)
        TextView articleTitle;
        @BindView(R.id.article_content)
        TextView articleContent;
        @BindView(R.id.txt_time)
        TextView txt_time;
        @BindView(R.id.btn_recover)
        TextView btnRecover;
        @BindView(R.id.btn_delete)
        TextView btnDelete;
        @BindView(R.id.swipemenu_view)
        SwipeMenuView swipemenuView;

        public RecyclerStationViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public void setOnItemFunctionClickListener(OnRecyclerStationItemClickListener listener) {
        this.listener = listener;
    }
}
